package de.bib.blue.manage.finance.jfree;

import java.awt.Color;
import java.util.Date;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.Month;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.ui.RectangleInsets;

public class TimeLineChart extends AbstractChart {

	public TimeLineChart(String title, String xLabel, String yLabel) {
		super(title, xLabel, yLabel);
		dataSet = new TimeSeriesCollection();
	}

	@Override
	public void createChart() {
		chart = ChartFactory.createTimeSeriesChart(title, // title
				xLabel, // x-axis label
				yLabel, // y-axis label
				(TimeSeriesCollection) dataSet, // data
				true, // create legend?
				true, // generate tooltips?
				false // generate URLs?
				);

		chart.setBackgroundPaint(Color.white);

		XYPlot plot = (XYPlot) chart.getPlot();
		plot.setBackgroundPaint(Color.lightGray);
		plot.setDomainGridlinePaint(Color.white);
		plot.setRangeGridlinePaint(Color.white);
		plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
		plot.setDomainCrosshairVisible(true);
		plot.setRangeCrosshairVisible(true);

		XYItemRenderer r = plot.getRenderer();
		if (r instanceof XYLineAndShapeRenderer) {
			XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
			renderer.setBaseShapesVisible(true);
			renderer.setBaseShapesFilled(true);
			renderer.setDrawSeriesLineAsPath(true);
		}

		/** set number y-axis. */
		NumberAxis range = (NumberAxis) plot.getRangeAxis();
		range.setRange(0.0, 100000000.0);

		DateAxis axis = (DateAxis) plot.getDomainAxis();
		axis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));

	}

	public void addTimeSeries(String label, List<Date> date, List<Number> values) {
		TimeSeries series = new TimeSeries(label);
		for (int i = 0; i < date.size(); i++) {
			Date d = date.get(i);
			series.add(new Month(d), values.get(i));
		}

		((TimeSeriesCollection) dataSet).addSeries(series);
	}

	@Override
	public void clearData() {
		((TimeSeriesCollection) dataSet).removeAllSeries();
	}

	@Override
	protected int getWidth() {
		return 900;
	}

	@Override
	protected int getHeight() {
		return 300;
	}

	public void saveChartIntoPNG(String pathFile) {
		try {
			ChartUtilities.saveChartAsPNG(new File(pathFile), chart,
					getWidth(), getHeight());
		} catch (IOException e) {
			throw new RuntimeException("Fail create file from chart because "
					+ e.getMessage());
		}
	}

	public void saveChartIntoJPEG(String pathFile) {
		try {
			ChartUtilities.saveChartAsJPEG(new File(pathFile), chart,
					getWidth(), getHeight());
		} catch (IOException e) {
			throw new RuntimeException("Fail create file from chart because "
					+ e.getMessage());
		}
	}
}
